package com.stars.easyms.alarm.client.email;

import com.stars.easyms.alarm.message.EasyMsAlarmMessage;
import com.stars.easyms.alarm.client.EasyMsAlarmClient;
import com.stars.easyms.alarm.constant.EasyMsAlarmConstant;
import com.stars.easyms.alarm.properties.EasyMsAlarmProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;

import java.util.stream.Stream;

/**
 * <p>className: EmailAlarmExecuteImpl</p>
 * <p>description: 邮箱告警</p>
 *
 * @author caotieshuan
 * @version 1.5.0
 * @date 2019-12-18 16:21
 */
@Slf4j
public final class EasyMsEmailAlarmClient implements EasyMsAlarmClient {

    private final EasyMsAlarmProperties.Email emailAlarmProperties;

    private String[] toUsers;

    @Override
    public void sendAlarmMessage(EasyMsAlarmMessage alarmMessage) throws EmailException {
        HtmlEmail htmlEmail = new HtmlEmail();
        htmlEmail.setCharset("UTF-8");
        htmlEmail.setHostName(emailAlarmProperties.getHost().trim());

        htmlEmail.setAuthentication(emailAlarmProperties.getFromUser().trim(), emailAlarmProperties.getToken().trim());
        htmlEmail.setFrom(emailAlarmProperties.getFromUser().trim());
        htmlEmail.addTo(toUsers);
        htmlEmail.setSubject(alarmMessage.getTitle());
        htmlEmail.setTextMsg(alarmMessage.getContext());
        if (StringUtils.isNotBlank(emailAlarmProperties.getSslPort().trim())) {
            htmlEmail.setSslSmtpPort(emailAlarmProperties.getSslPort().trim());
            htmlEmail.setSSLOnConnect(true);
            htmlEmail.setSSLCheckServerIdentity(true);
        } else {
            htmlEmail.setSmtpPort(Integer.parseInt(emailAlarmProperties.getPort().trim()));
        }
        String result = htmlEmail.send();
        if (log.isDebugEnabled()) {
            log.debug("email sendMsg success:" + result);
        }
    }

    @Override
    public String getClientType() {
        return EasyMsAlarmConstant.EMAIL;
    }

    @Override
    public boolean initAndReturnIsValid() {
        if (emailAlarmProperties.isEnabled()) {
            if (StringUtils.isNotBlank(emailAlarmProperties.getToUser())
                    && StringUtils.isNotBlank(emailAlarmProperties.getHost())
                    && StringUtils.isNotBlank(emailAlarmProperties.getPort())
                    && StringUtils.isNotBlank(emailAlarmProperties.getFromUser())
                    && StringUtils.isNotBlank(emailAlarmProperties.getToken())) {
                this.toUsers = Stream.of(emailAlarmProperties.getToUser().split(",")).map(String::trim).toArray(value -> new String[0]);
                return true;
            }
            log.error("Easy-ms {} alarm client is enabled, but the config is not valid!", getClientType());
        }
        return false;
    }

    public EasyMsEmailAlarmClient(EasyMsAlarmProperties alarmProperties) {
        this.emailAlarmProperties = alarmProperties.getEmail();
    }

}